/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.web;

import com.yunqian.redot.constant.Operation;
import com.yunqian.redot.constant.OperationMenus;
import com.yunqian.redot.domain.CoursesOrder;
import com.yunqian.redot.domain.Customers;
import com.yunqian.redot.domain.dto.CustomerDto;
import com.yunqian.redot.service.CustomersService;
import com.yunqian.redot.service.OperationLogService;
import com.yunqian.util.bean.BeanUtil;
import com.yunqian.util.poi.PoiExcelExport;
import com.yunqian.util.poi.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Describe: 客户信息表控制器
 *
 * @author zsy
 * 2018-09-29 15:54:28
 */
 @RestController
 @RequestMapping("/api/customers")
public class CustomersController {

     @Autowired
     private CustomersService customersService;
     @Autowired
     private OperationLogService operationLogService;

    /**
     * 获取分页
     *
     * @param params
     * @return
     */
    @PostMapping("/pagination")
    public Page<Customers> pagination(@RequestBody Map<String, Object> params) {
        return customersService.pagination(params);
    }

    /**
     * 导出excel
     *
     * @param req
     * @param resp
     */
    @RequestMapping("/export")
    public void export(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        operationLogService.save(OperationMenus.MEMBER_LIST, Operation.EXPORT, Long.valueOf(id));
        List<Customers> list = customersService.findAll();
        List<CustomerDto> dtoList = new ArrayList<>();
        list.forEach(item -> {
            CustomerDto dto = new CustomerDto();
            BeanUtil.copyNotNullProperties(dto,item);
            if(item.getGender() == 1){
                dto.setGenderStr("男");
            }else if(item.getGender() == 2){
                dto.setGenderStr("女");
            }
            dtoList.add(dto);
        });

        // 执行导出
        String fileName = new SimpleDateFormat("yyyyMMdd").format(new Date()) + "会员名单" + ".xls";
        ServletUtil su = new ServletUtil(fileName, req, resp);
        su.poiExcelServlet();
        String[] heads = {"序号", "手机号", "昵称", "性别"};
        String[] cols = {"id", "phone", "name", "genderStr"};
        // 这里传第几个字段是数字，从0开始
        int[] numerics = {0};
        ServletUtil suResp = new ServletUtil(resp);
        PoiExcelExport<CoursesOrder> pee = new PoiExcelExport(fileName, heads, cols, dtoList, numerics, suResp.getOut());
        pee.exportExcel();
    }

}
